package org.knowlege
import org.product.Product
import grails.converters.JSON

class KnowlegeController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

	def list = {
		//params.max = Math.min(params.max ? params.int('max') : 10, 100)
		//[knowlegeInstanceList: Knowlege.list(params), knowlegeInstanceTotal: Knowlege.count()]
		
		def product = Product.get(params?.productId?.toInteger())
		if(product){
			render view:"list",model:[productInstance:product,knowlegeInstanceList: Knowlege.findAllByProduct(product,[max:100, sort:"id", order:"desc", offset:0] )]
		}else{
		render view:"list",model:[productInstance:new Product(),knowlegeInstanceList: null]
		}
	}

    def create = {		
        def knowlegeInstance = new Knowlege()
        knowlegeInstance.properties = params
		knowlegeInstance.createUser = "admin";
		knowlegeInstance.product = Product.get(params.productId);
        return [knowlegeInstance: knowlegeInstance]
    }

    def save = {
        def knowlegeInstance = new Knowlege(params)
        if (knowlegeInstance.save(flush: true)) {
            flash.message = message(code: 'default.created.message', args: [message(code: 'knowlege.label', default: 'Knowlege'), knowlegeInstance.id])
            redirect(action: "show", id: knowlegeInstance.id)
        }
        else {
            render(view: "create", model: [knowlegeInstance: knowlegeInstance])
        }
    }

    def show = {
        def knowlegeInstance = Knowlege.get(params.id)
        if (!knowlegeInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'knowlege.label', default: 'Knowlege'), params.id])
            redirect(action: "list")
        }
        else {
            [knowlegeInstance: knowlegeInstance]
        }
    }

    def edit = {
        def knowlegeInstance = Knowlege.get(params.id)
        if (!knowlegeInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'knowlege.label', default: 'Knowlege'), params.id])
            redirect(action: "list")
        }
        else {
            return [knowlegeInstance: knowlegeInstance]
        }
    }

    def update = {
        def knowlegeInstance = Knowlege.get(params.id)
        if (knowlegeInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (knowlegeInstance.version > version) {
                    
                    knowlegeInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'knowlege.label', default: 'Knowlege')] as Object[], "Another user has updated this Knowlege while you were editing")
                    render(view: "edit", model: [knowlegeInstance: knowlegeInstance])
                    return
                }
            }
            knowlegeInstance.properties = params
            if (!knowlegeInstance.hasErrors() && knowlegeInstance.save(flush: true)) {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'knowlege.label', default: 'Knowlege'), knowlegeInstance.id])
                redirect(action: "show", id: knowlegeInstance.id)
            }
            else {
                render(view: "edit", model: [knowlegeInstance: knowlegeInstance])
            }
        }
        else {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'knowlege.label', default: 'Knowlege'), params.id])
            redirect(action: "list")
        }
    }

    def delete = {
        def knowlegeInstance = Knowlege.get(params.id)
        if (knowlegeInstance) {
            try {
                knowlegeInstance.delete(flush: true)
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'knowlege.label', default: 'Knowlege'), params.id])
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'knowlege.label', default: 'Knowlege'), params.id])
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'knowlege.label', default: 'Knowlege'), params.id])
            redirect(action: "list")
        }
    }
	
	def getListByProduct = {
		def product = Product.get(params?.productId)
		if(product){
			render view:"list",model:[knowlegeInstanceList: Knowlege.findByProduct(product)]
		}else{
		render view:"list",model:[knowlegeInstanceList: null]
		}
		
	}
	def listByType = {
		def product = Product.get(params?.productId)
		def knowlegeType= KnowlegeType.get(params?.knowlegeTypeId)
		if(product){
			render template:"knowleges",model:[knowlegeInstanceList: Knowlege.findAllWhere(product:product,knowlegeType:knowlegeType)]
		
		}
	
	}
}
